package my.structure.queue;

import my.structure.stack.impl.ListStack;

/**
 * @Author marshal
 * @Date 2021-08-01 10:57 PM
 *
 * 基于链表实现的队列
 */
public class ListQueue {

    private int[] items;
    private Node h = new Node();
    private int size;
    private Node head;
    private Node tail;


    private class Node {
        public int data;
        public Node next;

        public Node() {
        }

        public Node(int data) {
            this.data = data;
        }
    }

    public boolean enqueue(int e) {
        Node newNode = new Node(e);
        Node temp = h;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = newNode;
        tail = newNode;
        head = h.next;
        ++size;
        return true;
    }

    public int dequeue() {
        if (size == 0) {
            throw new RuntimeException("dequeue error!, this queue is empty");
        }
        Node temp = head;
        h.next = temp.next;
        head = h.next;
        --size;
        return temp.data;
    }

    public int size() {
        return size;
    }

}
